package aps.module_Machines;

//Java
import org.lwjgl.opengl.GL11;

//Minecraft
import net.minecraft.src.GuiContainer;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Block;

//Forge
import net.minecraftforge.client.MinecraftForgeClient;

//APS
import aps.BuildcraftAPS;


public class GuiMagmafier extends GuiContainer
{
	private TileEntityMagmafier magmafierInventory;
	String guiFile = BuildcraftAPS.imageFilesRoot + "MagmafierGUI.png";
	
	public GuiMagmafier(TileEntityMagmafier magmafier, InventoryPlayer playerInv)
	{
		super(new ContainerMagmafier(magmafier, playerInv));
        magmafierInventory = magmafier;
	}
	
	protected void drawGuiContainerForegroundLayer(int x, int y)
    {
        fontRenderer.drawString("Inventory", 8, (ySize - 96) + 2, 0x404040);
    }
	
	protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
    {
        int guiTex = mc.renderEngine.getTexture(guiFile);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        mc.renderEngine.bindTexture(guiTex);
        int topLeftX = (width - xSize) / 2;
        int topLeftY = (height - ySize) / 2;
        drawTexturedModalRect(topLeftX, topLeftY, 0, 0, xSize, ySize);
        displayBlockLevelGauge(topLeftX, topLeftY, 79, 11, (int) magmafierInventory.getScaledBlockQuantity(64));
        displayPowerLevelGauge(topLeftX, topLeftY, 93, 11, (int) magmafierInventory.getScaledPowerLevel(64));
        displayLiquidGauge(topLeftX, topLeftY, 107, 14, (int) magmafierInventory.getScaledLiquidQuantity(58), magmafierInventory.LavaID);
    }
	
	private void displayPowerLevelGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight)
	{
		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarHeight > 8) {
				x = 8;
				BarHeight -= 8;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 64 - x - start, 176, 8 - x, 8, x); //The 58 is the gauge height
			start = start + 8;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
    }
	
	private void displayBlockLevelGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight)
	{
		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarHeight > 16) {
				x = 16;
				BarHeight -= 16;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 64 - x - start, 176, 24 - x, 8, x); //The 58 is the gauge height
			start = start + 16;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
	}	
	
	private void displayLiquidGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight, int LiquidId)
	{
		Object o = Block.blocksList[LiquidId];
		int liquidImgIndex = Block.blocksList[LiquidId].blockIndexInTexture;
		
		if (o == null) {
			return;
		}

		mc.renderEngine.bindTexture(mc.renderEngine.getTexture("/terrain.png"));

		int imgLine = liquidImgIndex / 16;
		int imgColumn = liquidImgIndex - imgLine * 16;

		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarHeight > 16) {
				x = 16;
				BarHeight -= 16;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 58 - x - start, imgColumn * 16, (imgLine * 16) + (16 - x), 16, x); //The 58 is the gauge height
			start = start + 16;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
		mc.renderEngine.bindTexture(mc.renderEngine.getTexture(guiFile));
		drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY, 176, 24, 16, 60); //This draws the gauge measurement lines on top
    }
}